package com.tiancheng.trade.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.mapper.MerchantPaymentChannelMapper;
import com.tiancheng.trade.merchant.model.MerchantPaymentChannel;
import com.tiancheng.trade.merchant.service.IMerchantPaymentChannelService;
import com.tiancheng.trade.merchant.vo.merchant.GetMerchantPaymentVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantPaymentChannelService implements IMerchantPaymentChannelService {

    @Autowired
    private MerchantPaymentChannelMapper paymentMapper;

//    @Override
//    public MerchantPaymentChannel findById(int id) throws BusinessException {
//        return paymentMapper.selectById(id);
//    }
//
//    @Override
//    public List<MerchantPaymentChannel> findList(GetMerchantPaymentVO paymentVO) throws BusinessException {
//        QueryWrapper<MerchantPaymentChannel> conditions = new QueryWrapper<>();
//        conditions.eq("merchant_code", paymentVO.getMerchantCode());
//        conditions.orderByAsc("created_dt");
//        return paymentMapper.selectList(conditions);
//    }
//
//    @Override
//    public int insertBatch(List<MerchantPaymentChannel> paymentChannels) throws BusinessException {
//        return paymentMapper.insertBatch(paymentChannels);
//    }
//
//    @Override
//    public int updateDelBatch(List<String> list) throws BusinessException {
//        return paymentMapper.updateDelBatch(list);
//    }

    @Override
    public MerchantPaymentChannel findSubAppid(GetMerchantPaymentVO paymentVO) throws BusinessException {
        QueryWrapper<MerchantPaymentChannel> conditions = new QueryWrapper<>();
        conditions.eq("is_delete", 0);
        conditions.eq("payment_channel_code", paymentVO.getPaymentChannelCode());
        conditions.and(wrapper -> wrapper.eq("merchant_code", paymentVO.getMerchantCode()).or()
                .eq("merchant_code_business", paymentVO.getMerchantCode())
        );
        List<MerchantPaymentChannel> list = paymentMapper.selectList(conditions);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
